package ec.atreasure.com.compiler;

import com.squareup.javapoet.JavaFile;
import com.squareup.javapoet.TypeName;
import com.squareup.javapoet.TypeSpec;

import java.io.IOException;

import javax.annotation.processing.Filer;
import javax.lang.model.element.Modifier;
import javax.lang.model.type.TypeMirror;
import javax.lang.model.util.SimpleAnnotationValueVisitor7;

/**
 * Description :
 * Created by wangjin on 2019/6/28.
 * Job number：
 * Phone ：18301070822
 * Email： 120182051@qq.com
 * Person in charge : 汪渝栋
 * Leader：
 */
public class PayEntryVisitor extends SimpleAnnotationValueVisitor7<Void,Void> {
    private final Filer filer;

    private String mPackageName = null;

    public PayEntryVisitor(Filer filer) {
        this.filer = filer;
    }

    @Override
    public Void visitString(String s, Void o) {
        this.mPackageName = s;
        return o;
    }

    @Override
    public Void visitType(TypeMirror typeMirror, Void o) {
        generateJavaCode(typeMirror);
        return o;
    }

    private void generateJavaCode(TypeMirror typeMirror) {
        final TypeSpec targetActivity =
                TypeSpec.classBuilder("WXPayEntryActivity")
                        .addModifiers(Modifier.PUBLIC)
                        .addModifiers(Modifier.FINAL)
                        .superclass(TypeName.get(typeMirror))
                        .build();
        final JavaFile javaFile =
                JavaFile.builder(mPackageName + ".wxapi", targetActivity)
                        .addFileComment("wechat pay entry file")
                        .build();
        try {
            javaFile.writeTo(filer);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
